﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Jet.Payment.Cielo.Model
{
    public class PaymentType
    {
        public PaymentType()
        {
            PaymentDivisionNumbers = 1;
        }
        [XmlElement("bandeira")]
        public string TypeString { get { return GetTypePayment(Type); } set { Type = SetTypePayment(value); } }
        [XmlElement("produto")]
        public string ProductCieloString { get { return GetProduct(ProductCielo); } set { ProductCielo = SetProduct(value); } }
        [XmlIgnore]
        public Types Type { get; set; }
        [XmlIgnore]
        public ProductCielo ProductCielo { get; set; }
        [XmlElement("parcelas")]
        public int PaymentDivisionNumbers { get; set; }

        public bool Validate()
        {
            if (Type == Types.Discovery && ProductCielo != ProductCielo.Credito_a_vista)
                return false;
            if (ProductCielo == ProductCielo.Debito && Type != Types.Visa && Type != Types.Mastercard)
                return false;
            if ((ProductCielo == ProductCielo.Credito_a_vista || ProductCielo == ProductCielo.Debito) && PaymentDivisionNumbers != 1)
                return false;
            return true;
        }

        private Types SetTypePayment(string type)
        {
            switch (type)
            {
                case "visa":
                    return Types.Visa;
                case "mastercard":
                    return Types.Mastercard;
                case "amex":
                    return Types.AmericanExpress;
                case "elo":
                    return Types.Elo;
                case "diners":
                    return Types.Diners;
                case "discover":
                    return Types.Discovery;
                case "jcb":
                    return Types.JCB;
                case "aura":
                    return Types.Aura;
            }
            return default(Types);
        }
        private string GetTypePayment(Types type)
        {
            switch (type)
            {
                case Types.Visa:
                    return "visa";
                case Types.Mastercard:
                    return "mastercard";
                case Types.AmericanExpress:
                    return "amex";
                case Types.Elo:
                    return "elo";
                case Types.Diners:
                    return "diners";
                case Types.Discovery:
                    return "discover";
                case Types.JCB:
                    return "jcb";
                case Types.Aura:
                    return "aura";
                default:
                    return string.Empty;
            }
        }

        private string GetProduct(ProductCielo product)
        {
            switch (product)
            {
                case ProductCielo.Credito_a_vista:
                    return "1";
                case ProductCielo.Credito_parcelado_loja:
                    return "2";
                case ProductCielo.Credito_parcelado_emissor:
                    return "3";
                case ProductCielo.Debito:
                    return "A";
                default:
                    return null;
            }
        }

        private ProductCielo SetProduct(string product)
        {
            switch (product)
            {
                case "1":
                    return ProductCielo.Credito_a_vista;
                case "2":
                    return ProductCielo.Credito_parcelado_loja;
                case "3":
                    return ProductCielo.Credito_parcelado_emissor;
                case "A":
                    return ProductCielo.Debito;
                default:
                    return default(ProductCielo);
            }
        }
    }
}
